﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class BarZhanYong : Form
    {
        string cGxCode = "";
        string plCode = "";
        cMethods.DBHelper db;
        /// <summary>
        /// 操作类型
        /// </summary>
        /// <param name="dType"></param>
        /// <param name="keyCode"></param>
        public BarZhanYong(string dType,string keyCode)
        {
            InitializeComponent();
            db = new cMethods.DBHelper();
            cGxCode = dType;
            plCode = keyCode;
        }

        private void BarZhanYong_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "关联条码";
                ZhanYongBar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void ZhanYongBar()
        {
            StringBuilder sb = new StringBuilder();
            int gxIndex = 0;
            sb.Append("select * from pi_ProductionPlan_process where plCode='" + plCode + "'");
            sb.Append("order by gxOrder asc");
            var qTable = db.getData(sb.ToString(), "");
            string fGxCode = "";//上道工序编码
            for (int i = 0; i < qTable.Rows.Count; i++)
            {
                if ( qTable.Rows[i]["gxCode"].ToString().ToLower() == cGxCode.ToLower())
                {
                    if (gxIndex < 1)
                    {
                        fGxCode = cGxCode.ToLower();
                    }
                    else
                    {
                        fGxCode = qTable.Rows[i - 1]["gxCode"].ToString().ToLower();
                    }
                    break;
                }
                gxIndex++;
            }
            if (gxIndex < 1)
            {
                #region 第一道工序
                sb.Clear();
                sb.Append("select ceng,barCode,proCode,proName,spec,c_gongjin from V_ProductOccupy_info");
                sb.Append(" where isdel=0 and plCode='" + plCode + "' and baseCode='" + CommonMethods.baseCode + "'");
                #endregion
            }
            else
            {
                #region 不是第一道工序
                sb.Clear();
                sb.Append("select '' ceng,barCode,proCode,proName,spec,barStatus,c_gongjin from V_BarCode_Info ");
                sb.Append(" where gxCode='" + fGxCode + "' and planCode='" + plCode + "' and barStatus !='bar-drop'");
                sb.Append(" and baseCode='" + CommonMethods.baseCode + "'");
                #endregion
            }
            if (!string.IsNullOrWhiteSpace(tbBarCode.Text.ToString().Trim()))
            {
                sb.Append(" and barCode like '%" + tbBarCode.Text.ToString().Trim() + "%'");
            }
            if (!string.IsNullOrWhiteSpace(tbProCode.Text.ToString().Trim()))
            {
                sb.Append(" and proCode like '%" + tbProCode.Text.ToString().Trim() + "%'");
            }
            if (!string.IsNullOrWhiteSpace(tbProName.Text.ToString().Trim()))
            {
                sb.Append(" and proName like '%" + tbProName.Text.ToString().Trim() + "%'");
            }
            var table = db.getData(sb.ToString(), "");
            table.Columns.Add("mark");
            foreach (DataRow r in table.Rows)
            {
                sb.Clear();
                sb.Append("select top 1 barCode from bar_ProductBarCode_info ");
                sb.Append("where relationBar='"+ r["barCode"].ToString() +"'");
                var bqTable = db.getData(sb.ToString(),"");
                if (bqTable.Rows.Count > 0)
                {
                    r.BeginEdit();
                    r["mark"] = "1";
                    r.EndEdit();
                }
            }
            this.dvList.DataSource = table;
            this.dvList.AutoGenerateColumns = false;

    
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

    

        private void dvList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                CommonMethods.SinglePar = this.dvList.CurrentRow.Cells["barCode"].Value.ToString();
                CommonMethods.chooseWeight = this.dvList.CurrentRow.Cells["gongjin"].Value.ToString();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                ZhanYongBar();
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message) ;
               return;
            }
        }

        private void dvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < dvList.Rows.Count-1; i++)
            {
                var jg = dvList.Rows[i].Cells["mark"].Value.ToString();
                if (jg == "1")
                {
                    dvList.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
          
        }

        private void dvList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //var jg = this.dvList.Rows[e.RowIndex].Cells["mark"].Value;
            //if (jg == "1")
            //{ 
            
            //}
        }

      
    }
}
